<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GlassPublicModel
 *
 * @author zhuang
 */
class GlassPublicModel extends Model {

    //put your code here
    protected $tableName = 'glass';
    public $_default_post = array('query' => array('to_hide' => 1));

    public function importData() {
        $file_name = RUNTIME_PATH . 'Uploads/Excel/' . trim($_GET['file']) . '.csv';
        $file = fopen($file_name,'r'); 
        while ($data = fgetcsv($file)) {
            foreach($data as $key=>$val){
                $data[$key] = iconv('gbk','utf-8',$val);
            }
            $goods_list[] = $data;
        }
        fclose($file);
        $data=$this->getGlassData();
        foreach($goods_list as $key=>$val){
            if(empty($val[0])||$val[0]=='姓名'){
                continue;
            }
            foreach($data['Glass'] as $k=>$v){
                $tmp[$v[1]] = $val[$k];
            }
            $info[] = $tmp;
            unset($tmp);
        }
        $this->addDb($info);
        return array("error" => 0, "data" => '导入成功');
    }
    
    public function addDb($info){
        if(empty($info)){
            return ;
        }
        set_time_limit(0);
        foreach($info as $val){
            $this->data($val)->add();
        }
    }

    public function getGlassData(){
        $data   = array();
        array_push($data, array('姓名','user_name')); 
        array_push($data, array('性别','sex'));
        array_push($data, array('年龄','age'));
        array_push($data, array('配镜日期','instock_date'));
        array_push($data, array('职业','job'));
        array_push($data, array('电话','telphone'));
        array_push($data, array('球镜（R）','qiu_right'));
        array_push($data, array('柱镜（R）','zhu_right'));
        array_push($data, array('轴位（R）','zhou_right'));
        array_push($data, array('球镜（L）','qiu_left'));
        array_push($data, array('柱镜（L）','zhu_left'));
        array_push($data, array('轴位（L）','zhou_left'));
        array_push($data, array('瞳距','tong_right'));
        array_push($data, array('矫正视力','jiao_right'));
//        array_push($data, array('裸眼视力','luo_right'));
        array_push($data, array('镜架品牌与价格','frames'));
        array_push($data, array('镜片品牌与价格','eyeglass'));
        array_push($data, array('总金额','money'));
        array_push($data, array('备注','comments'));
        $info['Glass']  = $data;
        return $info;
    }

}

?>
